package com.example.demo.commons.core.util;

import com.example.demo.commons.result.common.enums.BaseErrorCodeEnum;
import com.example.demo.commons.result.common.exception.BizException;
import org.apache.commons.lang3.ObjectUtils;

import java.util.Collection;
import java.util.Map;

/**
 * <p> 断言 </p>
 *
 * @author tanyunpeng
 * @date 2021/11/18
 */
public class LocalAssertUtil {

    public static void isTrue(boolean expression, String message) {
        if (!expression) {
            throw BizException.create(BaseErrorCodeEnum.PARAMS_ERROR, message);
        }
    }

    public static void isTrue(boolean expression) {
        isTrue(expression, "[Assertion failed] - this expression must be true");
    }

    public static void isNull(Object object, String message) {
        if (object != null) {
            throw BizException.create(BaseErrorCodeEnum.PARAMS_ERROR, message);
        }
    }

    public static void isNull(Object object) {
        isNull(object, "[Assertion failed] - the object argument must be null");
    }

    public static void notNull(Object object, String message) {
        if (object == null) {
            throw BizException.create(BaseErrorCodeEnum.PARAMS_ERROR, message);
        }
    }

    public static void notNull(Object object) {
        notNull(object, "[Assertion failed] - this argument is required; it must not be null");
    }

    public static void hasLength(String text, String message) {
        if (text == null || text.isEmpty()) {
            throw BizException.create(BaseErrorCodeEnum.PARAMS_ERROR, message);
        }
    }

    public static void hasLength(String text) {
        hasLength(text, "[Assertion failed] - this String argument must have length; it must not be null or empty");
    }

    public static void hasText(String text, String message) {
        if (text == null || text.isEmpty() || !containsText(text)) {
            throw BizException.create(BaseErrorCodeEnum.PARAMS_ERROR, message);
        }
    }

    public static void hasText(String text) {
        hasText(text, "[Assertion failed] - this String argument must have text; it must not be null, empty, or blank");
    }

    public static void doesNotContain(String textToSearch, String substring, String message) {
        hasLength(textToSearch, message);
        hasLength(substring, message);
        if (!textToSearch.contains(substring)) {
            throw BizException.create(BaseErrorCodeEnum.PARAMS_ERROR, message);
        }
    }

    public static void doesNotContain(String textToSearch, String substring) {
        doesNotContain(textToSearch, substring,
                "[Assertion failed] - this String argument must not contain the substring [" + substring + "]");
    }

    public static void notEmpty(Object[] array, String message) {
        if (ObjectUtils.isEmpty(array)) {
            throw BizException.create(BaseErrorCodeEnum.PARAMS_ERROR, message);
        }
    }

    public static void notEmpty(Object[] array) {
        notEmpty(array, "[Assertion failed] - this array must not be empty: it must contain at least 1 element");
    }

    public static void noNullElements(Object[] array, String message) {
        if (array != null) {
            Object[] var2 = array;
            int var3 = array.length;

            for (int var4 = 0; var4 < var3; ++var4) {
                Object element = var2[var4];
                if (element == null) {
                    throw BizException.create(BaseErrorCodeEnum.PARAMS_ERROR, message);
                }
            }
        }
    }

    public static void noNullElements(Object[] array) {
        noNullElements(array, "[Assertion failed] - this array must not contain any null elements");
    }

    public static void notEmpty(Collection<?> collection, String message) {
        if (collection == null || collection.isEmpty()) {
            throw BizException.create(BaseErrorCodeEnum.PARAMS_ERROR, message);
        }
    }

    public static void notEmpty(Collection<?> collection) {
        notEmpty(collection,
                "[Assertion failed] - this collection must not be empty: it must contain at least 1 element");
    }

    public static void notEmpty(Map<?, ?> map, String message) {
        if (map == null || map.isEmpty()) {
            throw BizException.create(BaseErrorCodeEnum.PARAMS_ERROR, message);
        }
    }

    public static void notEmpty(Map<?, ?> map) {
        notEmpty(map, "[Assertion failed] - this map must not be empty; it must contain at least one entry");
    }

    private static boolean containsText(CharSequence str) {
        int strLen = str.length();
        for (int i = 0; i < strLen; i++) {
            if (!Character.isWhitespace(str.charAt(i))) {
                return true;
            }
        }
        return false;
    }
}

